import { addNewG2Shape } from '@/utils/g2'

let chart = null
const optValue = {
  isReflectX: false
}

const openReflect = (chartObj) => {
  updateOpt(chartObj, {
    reflect: optValue.isReflectX ? "" : "X",
  })
  optValue.isReflectX = !optValue.isReflectX
}

export const setCurrentChartObj = (chartObj) => {
  chart = chartObj
}

export function updateOpt(chartObj, opt) {
  chartObj.update(opt)
}

export const onEvent = (chartObj, event, cb) => {
  chartObj.on(event, cb)
}

export const btnGroups = {
  'openReflect': openReflect
}

export const registerDateHotMapShape = () => {
  addNewG2Shape("polygon", "boundary-polygon", {
    draw(cfg, container) {
      const group = container.addGroup();
      const attrs = {
        stroke: "#fff",
        lineWidth: 1,
        fill: cfg.color,
        paht: [],
      };
      const points = cfg.points;
      const path = [
        ["M", points[0].x, points[0].y],
        ["L", points[1].x, points[1].y],
        ["L", points[2].x, points[2].y],
        ["L", points[3].x, points[3].y],
        ["Z"],
      ];
      // @ts-ignore
      attrs.path = this.parsePath(path);
      group.addShape("path", {
        attrs,
      });

      if (cfg.data.lastWeek) {
        const linePath = [
          ["M", points[2].x, points[2].y],
          ["L", points[3].x, points[3].y],
        ];
        // 最后一周的多边形添加右侧边框
        group.addShape("path", {
          attrs: {
            path: this.parsePath(linePath),
            lineWidth: 4,
            stroke: "#404040",
          },
        });
        if (cfg.data.lastDay) {
          group.addShape("path", {
            attrs: {
              path: this.parsePath([
                ["M", points[1].x, points[1].y],
                ["L", points[2].x, points[2].y],
              ]),
              lineWidth: 4,
              stroke: "#404040",
            },
          });
        }
      }
      return group;
    },
  })
}


